const webpack = require('webpack');
const path = require('path');
// 覆盖默认antdDesign样式
const fs  = require('fs');

const lessToJs = require('less-vars-to-js');
const themeVariables = lessToJs(fs.readFileSync(path.join(__dirname, './src/static/css/ant-default-vars.less'), 'utf8'));

module.exports = {
  entry: [
    // 'react-hot-loader/patch',
    'webpack-dev-server/client?http://localhost:8080',
    'webpack/hot/only-dev-server',
    './index.js'
  ],
  output: {
    filename: '[name].js',
    chunkFilename: '[name].[chunkhash:5].chunk.js',
    path: path.join(__dirname, 'dist'),
    publicPath: '/'
  },

  resolve: {
    modules: ['node_modules', path.join(__dirname, 'node_modules')],
    extensions: ['*', '.web.js', '.js', '.json']
  },

  context:path.join(__dirname, 'src'),

  devServer: {
    hot: false,
    historyApiFallback: true,
    contentBase: path.join(__dirname, ''),
    publicPath: '/'
  },

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query:{
          "presets": [
            ["es2015", {"modules": false}], "stage-2", "react"
          ],
          "plugins": [
            ["import", { libraryName: "antd", style: true }
              // { "style": "css", "libraryName": "antd-mobile" }
            ],
            ["transform-runtime", {
              "helpers": false,
              "polyfill": false,
              "regenerator": true,
              "moduleName": "babel-runtime"
            }]
          ]

        }
      },
      {
        test: /\.css$/,
        use: [
          { loader: "style-loader" },
          { loader: "css-loader" },
        ],
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          { loader: 'css-loader', options: { importLoaders: 1 } },
          {loader: "less-loader",
            options: {
              modifyVars: themeVariables
            }
          }
        ]
      },
      {
        test: /\.(gif|png|jpe?g|svg)$/i,
        loader: 'url-loader?limit=8192'
      }, {
        test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: "url-loader?limit=10000&mimetype=application/font-woff"
      },
      {
        test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: "file-loader"
      }
    ],
  },

  plugins: [
    // enable HMR globally
    new webpack.HotModuleReplacementPlugin(),

    // prints more readable module names in the browser console on HMR updates
    new webpack.NamedModulesPlugin(),

    //jquerys
    new webpack.ProvidePlugin({
      $:"jquery",
      jQuery:"jquery",
      "window.jQuery":"jquery"
    }),

    //切换生产环境
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('dev')
      }
    }),

  ],
};